package com.xudow.app.ui;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.umeng.message.PushAgent;
import com.xudow.app.Config;
import com.xudow.app.R;
import com.xudow.app.XApplication;
import com.xudow.app.model.SimpleUser;
import com.xudow.app.ui.task.LogoutTask;
import com.xudow.app.util.PrefUtils;

import java.util.HashMap;

import static com.xudow.app.util.LogUtils.makeLogTag;

/**
 * 账号与安全Activity
 *
 * @author lukecheng
 */
public class SecurityActivity extends BaseActivity {
    private static final String TAG = makeLogTag(SecurityActivity.class);

    private TextView xuedouIdView;
    private String xuedouId;

    private LogoutTask logoutTask;

    private ToggleButton pushEnableToggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_security);

        xuedouId = getIntent().getStringExtra("xuedouId");

        xuedouIdView = (TextView) findViewById(R.id.xuedou_id);
        xuedouIdView.setText(xuedouId);

        pushEnableToggle = (ToggleButton) findViewById(R.id.push_enable_toggle);
        pushEnableToggle.setChecked(PrefUtils.isPushEnable(this));
    }

    public void onBackBtnClick(View view) {
        finish();
    }

    /**
     * 退出按钮点击事件
     *
     * @param view
     */
    public void onQuitClick(View view) {
        showLodingDialog(getString(R.string.logout_prompt));

        logoutTask = new LogoutTask(this, logoutTaskHandler);
        logoutTask.execute(new HashMap<String, String>());
    }

    /**
     * 修改密码点击
     *
     * @param view
     */
    public void onChangePasswordClick(View view) {
        Intent intent = new Intent(this, ForgetPasswordActivity.class);
        intent.putExtra("isChange", true);
        startActivity(intent);
    }

    /**
     * 清除缓存
     *
     * @param view
     */
    public void onCacheClearClick(View view) {
        showLodingDialog(getString(R.string.processing));
        cacheCleanHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                hideLoadingDialog();

                Toast.makeText(SecurityActivity.this, getString(R.string.cache_clean_error),
                        Toast.LENGTH_SHORT).show();
            }
        }, 2000);
    }

    private Handler cacheCleanHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
        }
    };

    /**
     * 客户热线点击
     *
     * @param view
     */
    public void onHotLineClick(View view) {
        String phone = ((TextView) findViewById(R.id.hotline_phone)).getText().toString();

        Intent intent = new Intent(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phone)));
        startActivity(intent);
    }

    /**
     * 退出登录处理Handler
     */
    private Handler logoutTaskHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            hideLoadingDialog();

            XApplication application = (XApplication) getApplication();
            application.setCookieStore(null);
            application.setRoles(null);

            final SimpleUser user = PrefUtils.getUserInfo(SecurityActivity.this);

            //关闭推送
            new Thread(new Runnable() {
                @Override
                public void run() {
                    PushAgent mPushAgent = PushAgent.getInstance(getApplicationContext());
                    try {
                        mPushAgent.removeAlias(user.getUsername(), Config.XUDOW_UMENG_MSG_ALIAS);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();

            //清除登录用户名
            user.setUsername("");
            user.setPassword("");
            PrefUtils.saveUserInfo(SecurityActivity.this, user);

            PrefUtils.setPrefLoginStatus(SecurityActivity.this, false);
            PrefUtils.saveHomeMessageTimeStamp(SecurityActivity.this, "");

            Intent intent = new Intent(SecurityActivity.this, LoginActivity.class);
            intent.putExtra("registerExitBroadcast", false);
            startActivity(intent);

            sendExitBroadcast();

            finish();
        }
    };

    /**
     * 是否允许推送点击
     *
     * @param view
     */
    public void onPushEnableClick(View view) {
        if (pushEnableToggle.isChecked()) {
            XApplication application = (XApplication) getApplication();
            if (application.isLogin()) {
                final SimpleUser user = PrefUtils.getUserInfo(this);

                //开启推送
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        PushAgent mPushAgent = PushAgent.getInstance(getApplicationContext());
                        try {
                            mPushAgent.addAlias(user.getUsername(), Config.XUDOW_UMENG_MSG_ALIAS);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        mPushAgent.enable();
                    }
                }).start();
            }

            PrefUtils.setPushEnable(this, true);
        } else {
            //关闭推送
            PushAgent mPushAgent = PushAgent.getInstance(getApplicationContext());
            mPushAgent.disable();

            PrefUtils.setPushEnable(this, false);
        }
    }
}
